﻿var version = "1.0"

function LoadCSS(url, isappendversion) {
    if (isappendversion) {
        url += "?version=" + version;
    }
    document.write('<link rel="stylesheet" href= ' + url + ' media="all"></link>');
}

function LoadJS(url, isappendversion) {
    if (isappendversion) {
        url += "?version=" + version;
    }
    document.write('<script src= ' + url + '></script>');
}

function GetRootPath() {
    var contextPath = GetCookie("jarmanage-contextPath");
    var loc = window.location,
        host = loc.hostname,
        protocol = loc.protocol,
        port = loc.port ? (':' + loc.port) : '';
    var path = location.pathname;

    var rootPath = protocol + '//' + host + port + contextPath;
    if(!rootPath.endsWith("/")){
        rootPath += "/"
    }
    var deviceID = sessionStorage.getItem("deviceID")
    if(deviceID!=null && deviceID!='localhost'){
        var deviceInfo = JSON.parse(sessionStorage.getItem("deviceInfo"));
        rootPath = deviceInfo.deviceVisitAddress
    }
    return rootPath;
}

function GetLocalRootPath() {
    var contextPath = GetCookie("jarmanage-contextPath");
    var loc = window.location,
        host = loc.hostname,
        protocol = loc.protocol,
        port = loc.port ? (':' + loc.port) : '';
    var path = location.pathname;

    var rootPath = protocol + '//' + host + port + contextPath;
    if(!rootPath.endsWith("/")){
        rootPath += "/"
    }
    return rootPath;
}

function GetCookie(cookieName) {
    //获取所有的cookie "psw=1234we; rememberme=true; user=Annie"
    var totalCookie = document.cookie;
    //获取参数所在的位置
    var cookieStartAt = totalCookie.indexOf(cookieName + "=");
    //判断参数是否存在 不存在直接返回
    if (cookieStartAt == -1) {
        return;
    }
    //获取参数值的开始位置
    var valueStartAt = totalCookie.indexOf("=", cookieStartAt) + 1;
    //以;来获取参数值的结束位置
    var valueEndAt = totalCookie.indexOf(";", cookieStartAt);
    //如果没有;则是最后一位
    if (valueEndAt == -1) {
        valueEndAt = totalCookie.length;
    }
    //截取参数值的字符串
    var cookieValue = unescape(totalCookie.substring(valueStartAt, valueEndAt));
    return cookieValue;
}

LoadJS(GetLocalRootPath() + 'plugin/jquery/jquery-1.10.2.min.js', false);
LoadJS(GetLocalRootPath() + 'plugin/jquery/jquery.validate.min.js', false);
LoadJS(GetLocalRootPath() + 'plugin/jquery/jquery.validate.message_cn.js', false);
LoadJS(GetLocalRootPath() + 'js/common.js', false);
LoadJS(GetLocalRootPath() + 'plugin/layui/layui.js', false);
LoadJS(GetLocalRootPath() + 'plugin/layuiextend/table.js', true);
LoadJS(GetLocalRootPath() + 'plugin/layuiextend/dialog.js', true);
LoadJS(GetLocalRootPath() + 'plugin/layuiextend/form.js', true);
LoadJS(GetLocalRootPath() + 'plugin/layuiextend/select.js', true);
LoadCSS(GetLocalRootPath() + 'css/common.css', true);
LoadCSS(GetLocalRootPath() + 'plugin/layui/css/layui.css', false);